﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using WFP_Sandbox.Annotations;
using WFP_Sandbox.Model;
using WFP_Sandbox.ViewModel;
using WFP_Sandbox.ViewModel.Helper;
using WFP_Sandbox.ViewModel.UI_Classes;

namespace WFP_Sandbox
{
    public class MainViewModel:ObservableObject
    {
        #region Properties

        #region DataTable
        private DataTable mPlayerTable = new DataTable();

        public DataTable PlayerTable
        {
            get { return mPlayerTable; }
            set
            {
                mPlayerTable = value;
                RaisePropertyChangedEvent("PlayerTable");
                
            }
        }
        #endregion

        #region Games
        private ObservableCollection<Game> mGames =new ObservableCollection<Game>();
        public ObservableCollection<Game> Games 
        {
            get { return mGames; }
            set
            {
                mGames = value;
                RaisePropertyChangedEvent("Games");
                RaisePropertyChangedEvent("PlayerTable");
            }

        }
        #endregion

        #region PlayerResults
        private ObservableCollection<PlayerResult> mPlayerResults = new ObservableCollection<PlayerResult>(); 
        public ObservableCollection<PlayerResult> PlayerResults
        {
            get { return mPlayerResults; }
            set
            {
                mPlayerResults = value;
                RaisePropertyChangedEvent("PlayerResults");
                RaisePropertyChangedEvent("PlayerTable");
            }
        }
        #endregion
        
        #region GamesShown
        private const int mGamesShown = 5;
        public int GamesShown
        {
            get { return mGamesShown; }
            
        }
        #endregion

        

        #endregion

        public  MainViewModel()
        {
            
            FetchModelData();
            FillDataTable();
            

        }

        private void FetchModelData()
        {
            var model = new MainModel();
            //convert Model data into appropiate ViewModel properties, Games and PlayerResults

            var tmpPlayerResults = new ObservableCollection<PlayerResult>(ConvertModelData.MapPlayerResultValues(model.GameValues).OrderBy(p => p.Sport));
            if (tmpPlayerResults.Any())
            {
                PlayerResults = tmpPlayerResults;
            }

            var tmpGames = new ObservableCollection<Game>(ConvertModelData.MapGameValues(model.GameValues).OrderBy(p => p.GameDate));
            if (tmpGames.Any())
                Games = tmpGames;
            
        }


        private void FillDataTable()
        {
            PlayerTable.Clear();
            
            PlayerTable.Columns.Add("Sport");
            PlayerTable.Columns.Add("Player");
            PlayerTable.Columns.Add("TeeNumber");

            for (int i = 1; i <= GamesShown; i++)
            {
                PlayerTable.Columns.Add(Games.ElementAt(i - 1).GameNumber);
            }

            foreach (var pResult in PlayerResults)
            {
                var tmpResult = pResult;
                var tmpRow = new string[3 + GamesShown];
                tmpRow[0] = tmpResult.Sport;
                tmpRow[1] = tmpResult.Name;
                tmpRow[2] = tmpResult.TeeNumber;
                for (int i = 0; i < GamesShown; i++)
                {
                    tmpRow[3 + i] = tmpResult.ContainsResultForGame(Games[i].GameNumber);

                }
                PlayerTable.Rows.Add(tmpRow);

            }


        }


        
    }
}
